import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:wanAndroid/bean/project_item_model.dart';
import 'package:wanAndroid/common/http/http_service.dart';
import 'package:wanAndroid/common/http/http_uts.dart';
import 'package:wanAndroid/common/uts/ui_uts.dart';
import 'package:wanAndroid/res/colors.dart';
import 'package:wanAndroid/widget/project_item_item_view.dart';

class PageProjectList extends StatefulWidget {
  String cid;
  bool isRefresh = true;

  @override
  State<StatefulWidget> createState() => _PageProjectList();

  static Widget start(String cid) {
    return PageProjectList(cid);
  }

  PageProjectList(this.cid, {this.isRefresh = true}) : assert(null != cid);
}

class _PageProjectList extends State<PageProjectList> {
  ///控制器
  RefreshController _refreshController;
  var _projectItemList = <ProjectItemDatas>[];
  int _projectPage = 0;

  ///页面总数
  var _pageCount = 0;

  ///是[否可以下拉
  bool _isLoading = true;

  ///显示加载
  bool showLoading = true;

  @override
  void initState() {
    _refreshController = RefreshController(initialRefresh: false);
    if (widget.isRefresh) {
      getProjectList(widget.cid);
    }
    super.initState();
  }

  @override
  void dispose() {
    _refreshController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) => Scaffold(
        backgroundColor: ResColors.scaffoldColor,
        appBar: null,
        body: Container(
          child: SmartRefresher(
            enablePullUp: _isLoading,
            enablePullDown: true,
            controller: _refreshController,
            header: ClassicHeader(),
            footer: ClassicFooter(),
            onRefresh: () async {
              setState(() {
                _projectPage = 1;
                _isLoading = true;
              });
              getProjectList(widget.cid);
            },
            onLoading: () async {
              if (_projectPage == _pageCount) {
                setState(() {
                  _isLoading = false;
                });
              }
              setState(() {
                _projectPage += 1;
              });
              getProjectList(widget.cid);
            },
            child:

                ///判断是否是空列表
                _projectItemList.isEmpty
                    ? UiUts.getEmpty()
                    : ListView.builder(
                        shrinkWrap: false,
                        itemBuilder: (context, pos) {
                          return ProjectItemItemView(_projectItemList[pos]);
                        },
                        itemCount: _projectItemList.length,
                      ),
          ),
        ),
      );

  ///项目列表
  Future<void> getProjectList(String projectCid) async {
    HttpService.projectList(
        _projectPage,
        projectCid,
        HttpCallback((resp) {
          UiUts.finishRefresh(_refreshController);

          ///关闭加载对话框
          setState(() {
            showLoading = false;
          });
          ProjectItemModel projectModel = ProjectItemModel.fromJson(resp.data);
          if (HttpUts.isSuccess(projectModel.errorCode)) {
            if (1 == _projectPage) {
              setState(() {
                showLoading = !showLoading;
                _pageCount = projectModel.data.pageCount;
                _projectItemList = projectModel.data.datas;
              });
            } else {
              setState(() {
                showLoading = !showLoading;
                _pageCount = projectModel.data.pageCount;
                _projectItemList.addAll(projectModel.data.datas);
              });
            }
          }
        }, (e) {
          UiUts.finishRefresh(_refreshController);

          ///关闭加载对话框
          setState(() {
            showLoading = false;
          });
        }));
  }
}
